package server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Vector;



public class BrowsingServiceImpl extends UnicastRemoteObject implements BrowsingService {
	private String databaseServer;
	private String rmiServerName;
	private String imagesFolder;
	private String serviceName;
	
	public BrowsingServiceImpl(File configFile)throws RemoteException{
		super(1098);
		processFile(configFile);
		
		System.getProperties().put("java.security.policy", "client.policy");
		System.getProperties().put("java.rmi.server.hostname", rmiServerName);
		
		if(databaseServer == null | rmiServerName == null | imagesFolder == null | serviceName == null){
			System.out.println("Incomplete configuration file, could not start server!");
			System.exit(1);
		}else{
			System.out.println("Configuration Complete");
			/* Boot registry */
			System.out.println("Booting the registry");
			try{
				Registry reg = LocateRegistry.createRegistry(1098);				/* Bind service to registry */
				reg.rebind("rmi://" + rmiServerName + "/" + serviceName, this);
			}catch(Exception e){
				e.printStackTrace();
				System.err.println("Could not boot RMI registry");
				System.exit(1);
			}	
		}
		System.out.println("System Now Booted");
	}
	
	private void processFile(File config) {
		/* First check file exists */
		if(!config.exists()){
			System.err.println("Could not file configuration file .config, please ensure .config is located in the install directory.");
			System.exit(1);
		}else{	
			System.out.println("Reading configuration file");
			
			/* Open text file */
			try {
				BufferedReader read = new BufferedReader(new FileReader(config));
				/* Read until we find match to user name */
				String currentLine;
				while((currentLine = read.readLine()) != null){
					if(currentLine.contains("#")){
						
					}else{
						String[] configOption = currentLine.split("=");
						
						/* Figure out which option it is */
						if(configOption[0].equalsIgnoreCase("mysql-server-host")){
							System.out.println("Configuring MySQL Host");
							databaseServer = configOption[1];
						}else if(configOption[0].equalsIgnoreCase("rmi-registry-host")){
							System.out.println("Configuring Registry Host");
							rmiServerName = configOption[1].substring(0, configOption[1].indexOf("/"));
							System.out.println("Server Name: " + rmiServerName);
						}else if(configOption[0].equalsIgnoreCase("image-file-path")){
							System.out.println("Configuring Image Output Folder");
							imagesFolder = configOption[1];
						}else if(configOption[0].equalsIgnoreCase("browsing-service")){
							System.out.println("Configuring Service Name");
							serviceName = configOption[1];
						}
					}
				}
				read.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Vector<ImageResult> search(String searchString)throws RemoteException {
		String databaseLocation = "jdbc:mysql://" + databaseServer + "/imagedatabase";
		Connection databaseCon = null;
		Vector<ImageResult> returnVector = new Vector<ImageResult>();
		ResultSet results;
		
		/* Register the MySQL database driver */
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			
			databaseCon = DriverManager.getConnection(databaseLocation, "imageupload", "imageupload");
						
			/* Create the Statement and exectue it */
			Statement uploadStatement = databaseCon.createStatement();
			System.out.println("Connected to database");
			results = uploadStatement.executeQuery("SELECT imageTitle, imageDescription, imageFileSize, imageCompression FROM images WHERE imageKeywords LIKE '%" + searchString  + "%'");
			
			/** Add the results to the Vector */
			while(results.next()){
			//	returnVector.add(new ImageResult(results.getString("imageTitle"), results.getString("imageDescription"), results.getString("imageFileSize"),results.getString("imageCompression")));
			}
			
			databaseCon.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnVector;
	}
	
	public static void main(String[] args){
		System.out.println("Starting Browsing Service");
		
		System.getProperties().put("java.security.policy", "client.policy");
		System.getProperties().put("java.rmi.server.hostname", "test-server.selfip.com");
		
		System.setSecurityManager(new java.rmi.RMISecurityManager());
		try {
			BrowsingServiceImpl service = new BrowsingServiceImpl(new File(".config"));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}

}
